﻿using System;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using System.Collections.Generic;

namespace Business
{
    public class VoucherHistory
    {
        private ISessionFactory _sessionFactory;
        public VoucherHistory()
        {
            _sessionFactory = DataObjects.Service.SessionFactory.GetInstance().GetNHibernateFactory();
        }

        public ArrayList GetListByVoucherNo(string VoucherNo)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select * from VOUCHER_HISTORY where VOUCHER_NO =?").AddEntity(typeof(DataObjects.VoucherHistory));
                query.SetString(0, VoucherNo);
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                return arrTemp;
            }
        }

        public ArrayList GetListByVoucherNoInvoiceNo(string VoucherNo, string InvoiceNo)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select * from VOUCHER_HISTORY where VOUCHER_NO ='" + VoucherNo + "' and INVOICE_NO = '" + InvoiceNo + "'").AddEntity(typeof(DataObjects.VoucherHistory));
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                return arrTemp;
            }
        }

        //code insert - update - delete 
        public void Insert(DataObjects.VoucherHistory oVoucherHistory)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Save(oVoucherHistory);
                tx.Commit();			        
            }
        }

        public void Update(DataObjects.VoucherHistory oVoucherHistory)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Update(oVoucherHistory);
                tx.Commit();
            }
        }

        public void DeleteByVoucherNoInvoiceNo(string VoucherNo, string InvoiceNo)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select * from VOUCHER_HISTORY where VOUCHER_NO ='" + VoucherNo + "' and INVOICE_NO = '" + InvoiceNo + "'").AddEntity(typeof(DataObjects.VoucherHistory));
                arrTemp = (ArrayList)query.List();

                foreach (DataObjects.VoucherHistory oVoucherHistory in arrTemp)
                {
                    session.Delete(oVoucherHistory);
                }
                tx.Commit();
            }
        }

        public void DeleteByVoucherNo(string VoucherNo)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select * from VOUCHER_HISTORY where VOUCHER_NO =?").AddEntity(typeof(DataObjects.VoucherHistory));
                query.SetString(0, VoucherNo);
                arrTemp = (ArrayList)query.List();
                
                foreach (DataObjects.VoucherHistory oVoucherHistory in arrTemp)
                {
                    session.Delete(oVoucherHistory);
                }
                tx.Commit();
            }
        }
    }
}
